package com.treeHole.treeHoleServer.controller;


import com.treeHole.treeHoleServer.pojo.RequestDataDTO.LoginDTO;
import com.treeHole.treeHoleServer.pojo.Result;
import com.treeHole.treeHoleServer.pojo.ResultDataDTO.ResultLoginDTO;
import com.treeHole.treeHoleServer.service.EmailService;
import com.treeHole.treeHoleServer.service.UserService;
import com.treeHole.treeHoleServer.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.plaf.PanelUI;
import java.util.HashMap;
import java.util.Map;

/**
 * 用于处理登录相关的请求
 */
@Slf4j
@RestController
public class LoginController {

    @Autowired
    EmailService emailService;

    @Autowired
    UserService userService;

    /**
     * 发送验证码的请求处理函数
     */
    @RequestMapping("/verify")
    public Result sendVerifyCode(@RequestBody LoginDTO loginDTO) {
        emailService.sendVerificationCode(loginDTO.getUserEmail());
        return Result.success();
    }

    @RequestMapping("/register")
    public Result newUserRegister(@RequestBody LoginDTO loginDTO) {
        if(emailService.verifyCode(loginDTO.getUserEmail(),loginDTO.getVerifyCode())) {
            Integer userId=userService.createUser(loginDTO.getUserEmail(),loginDTO.getPassword());
            if(userId==0) {return Result.error("used email!");}
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("email",loginDTO.getUserEmail());
            String jwt= JwtUtils.generateJwt(map);
            return Result.success(new ResultLoginDTO(userId,jwt));
        }else return Result.error("verify code error!");
    }

    @RequestMapping("/modifyPassword")
    public Result modifyPassword(@RequestBody LoginDTO loginDTO) {
        if(emailService.verifyCode(loginDTO.getUserEmail(),loginDTO.getVerifyCode())) {
            int resultCode= userService.modifyUserPassword(loginDTO.getUserEmail(),loginDTO.getNewPassword());
            return resultCode == 1 ? Result.success() : Result.error("修改密码失败!");
        }else return Result.error("verify code error!");
    }

    @RequestMapping("/loginByPassword")
    public Result loginByPassword(@RequestBody LoginDTO loginDTO) {
        Integer userId=userService.verifyPassword(loginDTO.getUserEmail(),loginDTO.getPassword());
        if(userId!=0) {
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("email",loginDTO.getUserEmail());
            String jwt= JwtUtils.generateJwt(map);
            return Result.success(new ResultLoginDTO(userId,jwt));
        }else return Result.error("wrong password!");
    }

    @RequestMapping("/loginByVerify")
    public Result loginByVerify(@RequestBody LoginDTO loginDTO) {
        if(emailService.verifyCode(loginDTO.getUserEmail(),loginDTO.getVerifyCode())){
            Integer userId=userService.getUserId(loginDTO.getUserEmail());
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("email",loginDTO.getUserEmail());
            String jwt= JwtUtils.generateJwt(map);
            return Result.success(new ResultLoginDTO(userId,jwt));
        }else return Result.error("verify code error!");
    }
}